/*
 * To change this license header, choose License Headers in Project Properties.
 * To change this template file, choose Tools | Templates
 * and open the template in the editor.
 */

package comandaXM.vista.auxiliars;
import java.util.Iterator;
import java.util.List;
import java.util.logging.Level;
import java.util.logging.Logger;
import comandaXM.accions.AuxiliarsAct; 
import comandaXM.classes.manteniment.auxiliar.TipusIva;
import comandaXM.classes.manteniment.auxiliar.TipusPagament;

import comandaXM.excepcions.ExceptionHibernate; 
import comandaXM.excepcions.ExceptionXML; 
import comandaXM.utils.JDialogXM1;
import comandaXM.utils.TDSLanguageUtils;
import comandaXM.utils.TableModel;
import comandaXM.utils.Utils;
import comandaXM.utils.eventXM.IListenerXM;
/**
 *
 * @author xmarti
 */
public abstract class JDTipusPagament extends JDialogXM1 {    
    private List<TipusPagament>pagaments;
    private TipusPagament tipusPagament;
    private AuxiliarsAct accio;
    private JPAuxiliarTipusPagament jPData;
    public JDTipusPagament(){        
        super(null,true,"JdTipusPagament",TDSLanguageUtils.getMessage("jDialogTipusPagament.JDialog.caption"));        
        this.accio=new AuxiliarsAct (); 
        this.jPData= new JPAuxiliarTipusPagament() {

            @Override
            public void movePosition(int e) {
                incrementarJTableXM(e);
            }
        };
        this.jPData.addKeyCodeListener(jPData);
        super.getjPanelData().setViewportView(this.jPData);
        try {
            super.chargeTable();
        } catch (ExceptionHibernate ex) {
            Logger.getLogger(JDTipusPagament.class.getName()).log(Level.SEVERE, null, ex);
        } catch (ExceptionXML ex) {
            Logger.getLogger(JDTipusPagament.class.getName()).log(Level.SEVERE, null, ex);
        }
        
        
    }

    @Override
    public TableModel getModel(String textToSearch) throws ExceptionHibernate {
       TableModel dadesTaula = null;                
            this.pagaments=(List<TipusPagament>) this.accio.getTipusPagament(textToSearch);
            String [] columns={"",
                          TDSLanguageUtils.getMessage("jDialogTipusPagament.JTable.codi.titol"),                               
                          TDSLanguageUtils.getMessage("jDialogTipusPagament.JTable.nom.titol"),
                          TDSLanguageUtils.getMessage("jDialogTipusPagament.JTable.dies.titol"),                          
                          TDSLanguageUtils.getMessage("jDialogTipusPagament.JTable.diaPagament.titol"),                          
                          TDSLanguageUtils.getMessage("jDialogTipusPagament.JTable.estat.titol")                          
                          };    
            Object[][] dades=null;
           
             if (this.pagaments.size()>0){
                dades=new Object[this.pagaments.size()][columns.length];                          
                Integer i=0;            
                for (Iterator it = this.pagaments.iterator(); it.hasNext();) {
                    TipusPagament c = (TipusPagament) it.next();                
                    dades[i][0]= c; 
                    if (c.getCodi() ==(null))dades[i][1]="-"; else dades[i][1]= c.getCodi();
                    if (c.getNom() ==(null))dades[i][2]="-"; else dades[i][2]= c.getNom();                    
                    dades[i][3]=c.getDies();  
                    dades[i][4]=c.getDiaPagament();
                    dades[i][5]=c.isActiu(); 
                    i++;
                }     
            }                  
        return new TableModel(columns, dades);   
    }

    @Override    
    public void getData() throws ExceptionHibernate  {                        
        this.tipusPagament.setNom(this.jPData.getNameAuxiliar());
        this.tipusPagament.setCodi(this.jPData.getCode());
        this.tipusPagament.setActiu(this.jPData.getEstat());
        this.tipusPagament.setDies(Utils.getInteger(this.jPData.getDies())); 
        this.tipusPagament.setDiaPagament(Utils.getInteger(this.jPData.getDiaPagament()));        
    }
    
    @Override
    public void setData() {
       
       if(super.isInsert()) this.tipusPagament=new TipusPagament();
       else this.tipusPagament=(TipusPagament) super.getObject();
       this.jPData.setNameAuxiliar(this.tipusPagament.getNom());                
       this.jPData.setCode(this.tipusPagament.getCodi()); 
       this.jPData.setDies(String.valueOf(this.tipusPagament.getDies() ));
       this.jPData.setDiaPagament(String.valueOf(this.tipusPagament.getDiaPagament()));
       this.jPData.setEstat(this.tipusPagament.isActiu());
       this.jPData.setRequestFocus();
    }
    @Override
    public void saveData() throws ExceptionHibernate  {                
        TipusPagament temp = new TipusPagament(tipusPagament.getId(),tipusPagament.getCodi(),tipusPagament.getNom(),tipusPagament.getDies(),tipusPagament.isActiu()) ; 
        temp.setDiaPagament(tipusPagament.getDiaPagament());
        
        getData();        
        try{
            if (super.isInsert()){
                this.tipusPagament.setId(this.accio.add(this.tipusPagament));

            }else{
                this.accio.update(this.tipusPagament);                
            }     
            this.setObject(this.tipusPagament);
            this.actualitzar();
        }catch (ExceptionHibernate ex){
            tipusPagament.setCodi(temp.getCodi());            
            tipusPagament.setNom(temp.getNom());
            tipusPagament.setDiaPagament(temp.getDiaPagament());
            tipusPagament.setDies(temp.getDies());
            tipusPagament.setActiu(temp.isActiu());
            setData(); 
            throw new ExceptionHibernate (ex.getMessage()); 
            //JOptionPane.showMessageDialog(null,ex.getMessage() ,TDSLanguageUtils.getMessage("save.data.ERR"),JOptionPane.ERROR_MESSAGE);
        }finally{
            temp=null; 
        }
        
    }
    @Override
    public boolean validation (){
        boolean temp=true; 
        if ("".equals(jPData.getCode())){
            jPData.setErrorCode(TDSLanguageUtils.getMessage("campObligatori"));             
            temp=false; 
        }
        if ("".equals(jPData.getDies())){
            jPData.setErrorDies(TDSLanguageUtils.getMessage("campObligatori"));            
            temp=false; 
        }        
        if ("".equals(jPData.getNameAuxiliar())){
            jPData.setErrorName(TDSLanguageUtils.getMessage("campObligatori"));            
            temp=false; 
        }
        return temp; 
    }
    @Override
    public void removeData() throws ExceptionHibernate {
        this.accio.delete(this.tipusPagament);
        super.setObject(new TipusIva());
        this.actualitzar();
    }

    @Override
    public boolean isSaved() {        
        //if (!this.isValid()) return true; 
        if (this.isInsert()) return true; 
        if(this.tipusPagament==null) return true;
        if (!this.tipusPagament.getNom().equals(this.jPData.getNameAuxiliar())) return false;                
        if (!this.tipusPagament.getCodi().equals(this.jPData.getCode())) return false ; 
        if(this.tipusPagament.isActiu()!= this.jPData.getEstat()) return false ;
        try{
            if(this.tipusPagament.getDies()!=Integer.parseInt(this.jPData.getDies()) ) return false; 
            if(this.tipusPagament.getDiaPagament()!=Double.parseDouble(this.jPData.getDiaPagament())) return false; 
        }catch(NumberFormatException nfe){
            return true ; 
        }
        
        return true;
    }
    private void incrementarJTableXM(int e){
        super.incrementarPosRegistre(e);
    }
    public abstract void actualitzar(); 
}
